import { defineStore } from "pinia";
import { ref } from "vue";
import { getUserInfoApi } from "@/apis/login";

export const useCookieStore = defineStore(
  "cookie",
  () => {
    const cookie = ref({});

    const info = ref({});

    const getUserInfo = async (id) => {
      const data = await getUserInfoApi({
        cookie: cookie.value.cookie,
        uid: id,
      });
      if (data.code !== 200)
        return uni.showToast({ title: "获取用户信息失败", icon: "fail" });
      info.value = data;
    };

    const cleanLoginInfo = () => {
      cookie.value = {}
      info.value = {}
    }

    return {
      cookie,
      info,
      getUserInfo,
      cleanLoginInfo
    };
  },
  {
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key);
        },
        setItem(key, value) {
          uni.setStorageSync(key, value);
        },
      },
    },
  }
);
